package homework.yuandan.service.impl;

import homework.yuandan.beans.Users;
import homework.yuandan.beans.dto.UserDTO;
import homework.yuandan.common.ResponseEnum;
import homework.yuandan.common.vo.ResultVO;
import homework.yuandan.config.TokenContainer;
import homework.yuandan.exception.AppException;
import homework.yuandan.mapper.UserMapper;
import homework.yuandan.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 作者:49293
 * 时间:2021/1/4-16:11
 * @author 49293
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private TokenContainer tokenContainer;
    @Override
    public ResultVO<UserDTO> check(String username, String password) {
        Users users = userMapper.selectByUsernameAndPassword(username);
        if (users ==null){
            throw new AppException(ResponseEnum.USER_NO_FOUND);
        }
        if (!password.equals(users.getPassword())){
            throw new AppException(ResponseEnum.PASSWORD_WRONG);
        }
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(users,userDTO);
        tokenContainer.createJavaWebToken(userDTO);
        return  new ResultVO<UserDTO>(ResponseEnum.SUCCESS_SERVER,userDTO);
    }
}
